#!/usr/bin/python3
# -*- coding:utf-8 -*-
# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
# This file is a part of the CANN Open Software.
# Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.
# ======================================================================================================================

import os
import numpy as np
import numpy as np


def edge32_hor_c3_sum(img, width, height):
    output = np.zeros_like(img)
    nr = 16
    x_start = 16
    x_end = width - 9
    for c in range(3):
        channel = img[:, :, c]
        kernel = np.ones(nr) / nr
        conv = np.apply_along_axis(
            lambda x: np.convolve(x, kernel, mode='valid'), 
            axis=1, 
            arr=channel
        )
        output_start = x_start - (nr // 2 - 1)
        output_end = output_start + (x_end - x_start)
        output[:, x_start:x_end, c] = conv[:, output_start:output_end]
    return output


def edge32_hor_c3(input_image, width, height):
    output = np.zeros((height, width), dtype=np.uint8)
    # 处理内部区域 (y: 1 to height-2, x: 15 to width-17)
    if height > 2 and width > 31:  # 确保有内部区域可处理
        top_pixels = input_image[0:height - 2, 15:width - 16, :].astype(np.int16)
        bottom_pixels = input_image[2:height, 15:width - 16, :].astype(np.int16)
        gradient = np.abs(bottom_pixels - top_pixels)
        gradient = np.minimum(gradient, 255)
        max_gradient = np.max(gradient, axis=2).astype(np.uint8)
        output[1:height - 1, 15:width - 16] = max_gradient
    return output


def edge_sub(edge1, edge1tmp1, width, height):
    diff = edge1tmp1.astype(np.int32) - edge1.astype(np.int32) - 10
    result = np.maximum(diff, 0)
    edge1tmp2 = result.astype(np.uint8) 
    return edge1tmp2


def edge_dilate_hor_c1(input_image, width, height):
    output = np.zeros_like(input_image)
    output[:2, :] = 0
    output[-2:, :] = 0
    top = input_image[1:-3]
    center = input_image[2:-2]
    bottom = input_image[3:-1]
    dilated = np.maximum(top, np.maximum(center, bottom))
    output[2:-2, :] = dilated
    return output


def threshol8u(img, width, height):
    return np.where(img != 0, 1, 0).astype(np.uint8)


def gen_golden_data_simple():
    dtype = np.uint8
    height = 3440
    width = 4887
    img = np.random.randint(0, 256, [height, width, 3]).astype(dtype)
    edge3 = np.random.randint(0, 256, [height, width]).astype(dtype)
    edge3tmp1 = edge32_hor_c3_sum(img, width, height)
    edge3tmp2 = edge32_hor_c3(edge3tmp1, width, height)
    edge3tmp3 = edge_sub(edge3, edge3tmp2, width, height)
    edge3tmp4 = edge_dilate_hor_c1(edge3tmp3, width, height)
    dst3 = threshol8u(edge3tmp4, width, height)

    os.system("mkdir -p input")
    os.system("mkdir -p output")
    img.astype(dtype).tofile("./input/input_Img.bin")
    edge3.astype(dtype).tofile("./input/input_Edge3.bin")
    dst3.tofile("./output/golden.bin")

if __name__ == "__main__":
    gen_golden_data_simple()

